package com.linkwedo.saas.users;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.linkwedo.saas.domain.member.Team;
import com.linkwedo.saas.domain.member.User;
import com.linkwedo.saas.util.DateUtil;
import com.opensymphony.xwork2.ActionSupport;


public class AddTeamAction extends ActionSupport implements ServletRequestAware{
	private TeamService teamService;
	private UserService userService;
	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}
	private Integer id;
	private String name;
	private String createTime;
	private String[] userids;
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getProjectId() {
		return projectId;
	}

	public void setProjectId(Integer projectId) {
		this.projectId = projectId;
	}
	private String description;
	private Integer projectId;

	public javax.servlet.http.HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(javax.servlet.http.HttpServletRequest request) {
		this.request = request;
	}

	private javax.servlet.http.HttpServletRequest request;
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
	}

	private List<Team> list;
	public List<Team> getList() {
		return list;
	}

	public void setList(List<Team> list) {
		this.list = list;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	private static final long serialVersionUID = 8190003683775267839L;
	public String execute() throws Exception {
		name=request.getParameter("name");
		description=request.getParameter("description");
		createTime=request.getParameter("createTime");
		description=request.getParameter("description");
		userids=request.getParameterValues("userids");
		Set users = new HashSet();
		if(null != userids && userids.length>0){
			for(int i=0;i<userids.length;i++){
				Integer curUserId = Integer.parseInt(userids[0]);
				User curUser = getUserService().getUserById(curUserId);
				users.add(curUser);
			}
		}
		Team pro=new Team();
		pro.setCreateTime(DateUtil.str2Timestamp(createTime));
		pro.setDescription(description);
		pro.setName(name);
		pro.setProjectId(projectId);
		pro.setUsers(users);
		getTeamService().save(pro);
		list=getTeamService().findAll();
		return "ok";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String[] getUserids() {
		return userids;
	}

	public void setUserids(String[] userids) {
		this.userids = userids;
	}
	
	}
